package com.onesports.intelligent.k12.polarlight.controller.organization.staff;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onesports.framework.kit.common.valid.Json;
import com.onesports.intelligent.k12.polarlight.common.utils.ResultUtil;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.constant.SystemCodeConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.IdDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.RoleDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.UseDataScopeIdDTO;
import com.onesports.intelligent.k12.polarlight.domain.vo.common.AuthTreeVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.DataScopeTableVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.RoleVO;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.sys.MenuService;
import com.onesports.intelligent.k12.polarlight.service.sys.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.lang.reflect.Array;
import java.util.List;

@Api(tags = "机构端-机构管理-角色管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.ORGANIZATION_ROLE)
public class OrganizationRoleController {

    private final RoleService roleService;
    private final MenuService menuService;

    @PostMapping("/add")
    @ApiOperation("添加")
    public ResponseEntity<String> add(@Json @RequestBody RoleDTO dto) {
        dto.setIsSuper(false);
        this.roleService.add(dto, SecurityUtils.getOrganizationId(),SystemCodeConstants.ORGANIZATION);
        return ResultUtil.success("添加成功");
    }


    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseEntity<String> del(@Json @RequestBody IdDTO dto) {
        this.roleService.delete(dto.getId());
        return ResultUtil.success("删除成功");
    }


    @PostMapping("/update")
    @ApiOperation("编辑")
    public ResponseEntity<String> update(@Json @RequestBody RoleDTO dto) {
        dto.setIsSuper(false);
        this.roleService.edit(dto, SecurityUtils.getOrganizationId(),SystemCodeConstants.ORGANIZATION);
        return ResultUtil.success("操作成功");
    }


    @GetMapping("/page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "row", value = "条数", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "roleName", value = "角色名称", dataTypeClass = String.class, paramType = "query")
    })
    public ResponseEntity<IPage<RoleVO>> page(@NotEmpty(message = "页数不可为空") Long page, @NotEmpty(message = "条数不可为空") Long row, String roleName) {
        return ResultUtil.success(this.roleService.page(new Page<>(page, row), roleName, SecurityUtils.getOrganizationId()));
    }


    @GetMapping("/auth")
    @ApiOperation("编辑页角色已有权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "systemCode", value = "系统编码", required = true, dataTypeClass = String.class, paramType = "query")
    })
    public ResponseEntity<List<AuthTreeVO>> auth(@NotEmpty(message = "角色id不可为空") String id,
                                                 @NotEmpty(message = "系统编码不可为空") String systemCode) {
        return ResultUtil.success(this.roleService.auth(id, systemCode));
    }


    @ApiOperation("添加页面菜单树")
    @GetMapping("/tree")
    public ResponseEntity<List<AuthTreeVO>> getTree() {
        return ResultUtil.success(this.menuService.getTree(SystemCodeConstants.ORGANIZATION));
    }

    @ApiOperation("添加页面数据权限表格")
    @PostMapping("/data-scope-table")
    public ResponseEntity<DataScopeTableVO> getDataScopeTable(@RequestBody(required = false) UseDataScopeIdDTO useDataScopeIds){
        return ResultUtil.success(roleService.getDataScopeTable(SecurityUtils.getOrganizationId(),useDataScopeIds.getUseDataScopeIds()));
    }

}
